package com.huluobo.a2109mvpimdemo.mine;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.huluobo.a2109mvpimdemo.R;
import com.huluobo.a2109mvpimdemo.adapter.MyFragmentAdapter;
import com.huluobo.a2109mvpimdemo.base.BaseFragment;
import com.huluobo.a2109mvpimdemo.login.LoginActivity;
import com.hyphenate.chat.EMClient;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.List;

public class MineFragment extends BaseFragment<MineContract.View, MinePresenter> implements MineContract.View {
    private ImageView QRCode;
    @Override
    protected int bindingLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        presenter = new MinePresenter();
    }

    @Override
    protected void initView() {
        Button logout = findViewById(R.id.mine_logout);
        logout.setOnClickListener(v -> {
            presenter.logout();
        });
        //String.format中第一个参数是从资源文件中拿到需要修改的字符串,第二个参数是当前登录的用户
        //format会找到第一个参数中%s的位置,将第二个参数替换到对应位置
        //下面2种方式都是替换字符串,第一种是从string这个xml中拿到字符串,第二种是直接写一个String来替换
        String username = String.format(getString(R.string.logout), EMClient.getInstance().getCurrentUser());
//        String username = String.format("退出(%s)登录", EMClient.getInstance().getCurrentUser());
//
//        String username1 = EMClient.getInstance().getCurrentUser();
//        logout.setText("退出(" + username1 + ")登录");
        logout.setText(username);

        QRCode =findViewById(R.id.mine_QRCode);
        String str = EMClient.getInstance().getCurrentUser();

        Bitmap logo = BitmapFactory.decodeResource(getResources(),R.mipmap.wechat);
        Bitmap image = CodeUtils.createImage(str,400,400,logo);
        QRCode.setImageBitmap(image);

    }

    @Override
    public void onLogoutSuccess() {
        if (getActivity() != null) {
            dismissProgressDialog();
            getActivity().runOnUiThread(() -> {
                Toast.makeText(getActivity(), "退出登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            });
        }
    }

    @Override
    public void onLogoutFailed(String error) {
        if (getActivity() != null) {
            dismissProgressDialog();
            getActivity().runOnUiThread(() -> Toast.makeText(getActivity(), "退出登录失败:" + error, Toast.LENGTH_SHORT).show());
        }
    }

    @Override
    public void onStartLogout() {
        showProgressDialog("正在退出...");
    }
}